// pages/order/order.js
import Dialog from '/@vant/weapp/dialog/dialog';
import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 'one',
    // 全部
    orderAllList: [],
    // 待付款
    orderPayList: [],
    // 待收货
    orderConsigneeList: [],
    // 待评价
    orderEvaluationList: [],
    // 售后
    orderCustomerList: []


  },

  changeTabs(e) {
    console.log(e)
    const active = e.detail.activeKey
    // wx.showToast({
    //   title: `切换到标签 ${e.detail.activeKey}`,
    //   icon: 'none'
    // });
    switch (active) {
      case 'one':
        this.queryOrder('all')
        break;
      case 'two':
        this.queryOrder('0')
        break;
      case 'three':
        this.queryOrder('1')
        break;
      case 'four':
        this.queryOrder('2')
        break;
      case '3':
        this.queryOrder('3')
        break;
    } 
  },
  /**
   * 查询订单
   */
  queryOrder(status) {
    let openId = wx.getStorageSync('openId');
    http.request({
      url: `order-master/getList/${openId}/${status}`,
      success: (res) => {
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        for(let i=0;i<res.data.length;i++){
          let time =  Date.parse(res.data[i].createTime.replace(/-/g, '/'))/1000
          if(timestamp - time > 60 * 60 * 1){
            res.data[i].oneTime = false

          }else{
            res.data[i].oneTime = true
          }

          console.log(time)

        };

        switch (status) {
          case 'all':
            this.setData({
              orderList: res.data
            })
            break;
          case '0':
            this.setData({
              orderPayList: res.data
            })
            break;
          case '1':
            this.setData({
              orderConsigneeList: res.data
            })
            break;
          case '2':
            this.setData({
              orderEvaluationList: res.data
            })
            break;
          case '3':
            this.setData({
              orderCustomerList: res.data
            })
            break;
        } 
        
      }
    })
  },
  removeOrderClick(event){
    const orderId = event.currentTarget.dataset.orderid;
    Dialog.confirm({
      title: '提示',
      message: '确定删除此订单吗？'
    }).then(() => {
      this.removeOrderById(orderId);
    }).catch(() => {
      // on cancel
    });
  },
  removeOrderById(orderId){
    http.request({
      url: 'order-master/' + orderId,
      method: 'delete',
      success: (res) => {
        const active = this.data.active;
        debugger
        this.queryOrder('1')
        if(active == 'one'){
          this.queryOrder('all')
        }else{
          this.queryOrder('0')
        }
      }
    })
  },
  goToPayClick(event){
    const orderId = event.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: "/pages/orderAccount/orderAccount?orderId=" + orderId
    });
  },
  goToOrderInfoClick(event){
    const orderId = event.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: "/pages/orderInfo/orderInfo?orderId=" + orderId
    });
  },
  /**
   * 提醒发货
   */
  addRemindNum(){
    http.request({
      url: 'order-master/addRemindNum/' + orderId,
      method: 'put',
      success: (res) => {
        wx.showToast({
          title: '提醒成功',
          icon: 'none'
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const active = options.active
    if(active){
      this.setData({
        active
      })
    }
    switch (active) {
      case 'one':
        this.queryOrder('all')
        break;
      case 'two':
        this.queryOrder('0')
        break;
      case 'three':
        this.queryOrder('1')
        break;
      case 'four':
        this.queryOrder('2')
        break;
      case '3':
        this.queryOrder('3')
        break;
    } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})